package com.carkeeper.storebill.service.impl;

import com.carkeeper.entity.Result;
import com.carkeeper.pojo.Employee;
import com.carkeeper.storebill.dao.StoreBillLogMapper;
import com.carkeeper.storebill.dao.StoreBillMapper;
import com.carkeeper.storebill.pojo.StoreBill;
import com.carkeeper.storebill.pojo.StoreBillLog;
import com.carkeeper.storebill.pojo.StoreBillShow;
import com.carkeeper.storebill.service.StoreBillLogService;
import com.carkeeper.storebill.service.StoreBillService;
import com.carkeeper.subject.feign.StoreFeign;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.subject.pojo.StoreFull;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class StoreBillServiceImpl implements StoreBillService {

    @Autowired
    StoreBillMapper storeBillMapper;

    @Autowired
    StoreBillLogMapper storeBillLogMapper;

    @Autowired
    StoreFeign storeFeign;

    @Autowired
    StoreBillLogService storeBillLogService;


    /**
     * 根据条件查询
     * 账单编号 商家名称 支付状态 账单起始时间 账单结束时间
     * @param searchMap 查询条件集合
     * @return 账单实体集合
     */
    @Override
    public PageInfo<StoreBill> findByCondition(Map<String, String> searchMap) {

        Example example = new Example(StoreBill.class);
        Example.Criteria criteria = example.createCriteria();

        int pageSize = 20;
        int pageNum = 1;

        if (searchMap != null){

            if (searchMap.get("startPage") != null && searchMap.get("pageSize") != null){

                if (!"".equals(searchMap.get("startPage"))){
                    pageNum = Integer.parseInt(searchMap.get("startPage"));
                }

                if (!"".equals(searchMap.get("pageSize"))){
                    pageSize = Integer.parseInt(searchMap.get("pageSize"));
                }
            }

            //订单编码
            if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))){

                criteria.andEqualTo("code", searchMap.get("code"));
            }
            //商家名称
            if (searchMap.get("storeName") != null && !"".equals(searchMap.get("storeName"))){

                criteria.andEqualTo("storeName", searchMap.get("storeName"));
            }
            //支付状态
            if (searchMap.get("payStatus") != null && !"".equals(searchMap.get("payStatus"))){

                criteria.andEqualTo("payStatus", searchMap.get("payStatus"));
            }
            //起始时间
                if (searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime"))){

                criteria.andGreaterThanOrEqualTo("createTime", searchMap.get("startTime"));
            }
            //终止时间
            if (searchMap.get("endTime") != null && !"".equals(searchMap.get("endTime"))){

                criteria.andLessThanOrEqualTo("createTime", searchMap.get("endTime"));
            }
        }

        PageHelper.startPage(pageNum,pageSize);

        List<StoreBill> storeBills = storeBillMapper.selectByExample(example);

        PageInfo<StoreBill> info = new PageInfo<>(storeBills);


        return info;
    }

    /**
     * 根据code查询账单信息
     * @param code 账单编码
     * @return 账单,车主，商家组合实体（Map）
     */
    @Override
    public Map<String,Object> findByCode(String code) {

        Example example = new Example(StoreBill.class);
        Example.Criteria criteria = example.createCriteria();
        Map<String,Object> resultMap = new HashMap();
        List<StoreBill> storeBills = new ArrayList<>();
        if (code != null){
            criteria.andEqualTo("code",code);
            storeBills = storeBillMapper.selectByExample(example);
        }


        if (storeBills == null || storeBills.get(0) == null){

            throw new RuntimeException("查询的账单不存在");
        }
        resultMap.put("storeBill",storeBills.get(0));


        String storeId = storeBills.get(0).getStoreId();
        Integer id = storeBills.get(0).getId();

        if (StringUtils.isNotEmpty(storeId)){
            //此处查询收款方
            Store store = storeFeign.findById(storeId).getData().getStore();
            //查询平台信息 平台id：1
            Store paltform = storeFeign.findById("1").getData().getStore();

            //封装进map
            resultMap.put("platform",store);
            resultMap.put("beneficiary",store);

            //查询日志信息
            List<StoreBillLog> storeBillLogs = storeBillLogService.findListByBillId(id);
            if (storeBillLogs != null && storeBillLogs.size() > 0){

                resultMap.put("storeBillLogs",storeBillLogs);
            }
        }

        return resultMap;
    }

    /**
     * 查询所有的支付状态
     * @return 支付状态的集合
     */
    @Override
    public List<String> findPayStatusList() {

        List<String> payStatusList = storeBillMapper.findPayStatusList();

        return payStatusList;
    }

    /**
     * 以商家聚合查询所有商家账单概要
     * @return
     * 用于服务商账单支付页面数据初始化
     */
    @Override
    public PageInfo<StoreBillShow> findShowStoreBill(Map<String,String> searchMap) {

        int pageSize = 20;
        int pageNum = 1;

        if (searchMap != null){

            if (searchMap.get("startPage") != null && searchMap.get("pageSize") != null){

                if (!"".equals(searchMap.get("startPage"))){
                    pageNum = Integer.parseInt(searchMap.get("startPage"));
                }

                if (!"".equals(searchMap.get("pageSize"))){
                    pageSize = Integer.parseInt(searchMap.get("pageSize"));
                }

                PageHelper.startPage(pageNum,pageSize);
            }
        }

        List<StoreBillShow> showStoreBill = storeBillMapper.findShowStoreBill(searchMap);

        PageInfo<StoreBillShow> info = new PageInfo<>(showStoreBill);

        return info;
    }

    /**
     * 根据商家id查询所有未支付的账单
     * @param storeId
     * @return 账单集合
     */
    @Override
    public List<StoreBill> findNotPayStoreBillByStoreId(Integer storeId,Map<String,String> searchMap) {

        Example example = new Example(StoreBill.class);
        Example.Criteria criteria = example.createCriteria();
        Map<String,Object> resultMap = new HashMap();
        List<StoreBill> storeBills = new ArrayList<>();

        if (searchMap != null){

            //账单编号
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))){

                criteria.andEqualTo("id", searchMap.get("id"));
            }
            //账单类型
            if (searchMap.get("type") != null && !"".equals(searchMap.get("type"))){

                criteria.andEqualTo("type", searchMap.get("type"));
            }
            //起始时间
            if (searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime"))){

                criteria.andGreaterThanOrEqualTo("createTime", searchMap.get("startTime"));
            }
            //终止时间
            if (searchMap.get("endTime") != null && !"".equals(searchMap.get("endTime"))){

                criteria.andLessThanOrEqualTo("createTime", searchMap.get("endTime"));
            }
        }

        criteria.andEqualTo("storeId",storeId);

        return storeBillMapper.findNotPayStoreBillByStoreId(storeId);
    }

    @Override
    public void PayStoreBillByCode(String code) {

        Example example = new Example(StoreBill.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("code",code);

        StoreBill storeBill = storeBillMapper.selectOneByExample(example);

        try {
            if ("1".equals(storeBill.getPayStatus())){

                throw new RuntimeException("账单已支付");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ;
        }

        storeBill.setPayStatus("1");

        storeBillMapper.updateByPrimaryKey(storeBill);

        StoreBillLog storeBillLog = new StoreBillLog();
        storeBillLog.setBillId(storeBill.getId());
        storeBillLog.setBillStatus(new Integer(storeBill.getPayStatus()));
        storeBillLog.setType("支付账单");

        //封装用户的信息

       storeBillLogService.insertBillLog(storeBillLog);
    }

}
